<?php

    class Application_Plugin_SocialPlugin extends Zend_Controller_Plugin_Abstract
    {
        private $request;
        public function preDispatch(Zend_Controller_Request_Abstract $request)
        {
            $x = new Zend_Auth_Storage_Session('User_Auth');
            $y = $x->read();
            $this->request = $request;
            if (empty($y))
            {
                $this->facebookLogin();
            }
        }

        public function facebookLogin()
        {
            require_once APPLICATION_PATH . '/../assets/social/facebook/facebook.php';
            $facebook_auth_array = array();

// Create our Application instance (replace this with your appId and secret).
            $facebook = new Facebook(array(
                'appId' => FACEBOOK_APP_ID,
                'secret' => FACEBOOK_SECRET_ID,
            ));

// Get User ID
            $user = $facebook->getUser();

// Login or logout url will be needed depending on current user state.
            if ($user)
            {
                $logoutUrl = $facebook->getLogoutUrl();
            }
            else
            {
                $loginUrl = $facebook->getLoginUrl(array(
                    "redirect_uri" => FACEBOOK_CALLBACK_URL,
                    "scope" => "email"
                ));
            }

            
            if(!empty($this->request->pswa) && !empty($this->request->user_id))
            
            $facebook_auth_array = array(
                "login_url" => $facebook->getLoginUrl(array(
                    "redirect_uri" => FACEBOOK_CALLBACK_URL."/pswa/".$this->request->pswa."/user_id/".$this->request->user_id,
                    "scope" => "email"
                )),
                "logout_url" => $facebook->getLogoutUrl()
            );
            else
                $facebook_auth_array = array(
                "login_url" => $facebook->getLoginUrl(array(
                    "redirect_uri" => FACEBOOK_CALLBACK_URL,
                    "scope" => "email"
                )),
                "logout_url" => $facebook->getLogoutUrl()
            );
            
            //prd($facebook_auth_array);
            
            Zend_Registry::set("FACEBOOK_AUTH", $facebook_auth_array);
        }

        

    }

?>